package com.book.remote;

import com.book.dto.BookDto;
import com.book.dto.BrowseHistoryDto;
import com.book.helper.BrowseHistoryHelper;
import com.book.pojo.BrowseHistory;
import com.book.service.BookService;
import com.book.service.BrowseHistoryService;
import com.book.service.ImgService;
import com.book.vo.BrowseHistoryVo;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Author Jincheng.Li
 * @Date 2021/5/29 9:20 下午
 * @Project_Name graduation-project
 */
@Component
public class BrowseHistoryRemote {
    @Autowired
    private BrowseHistoryService browseHistoryService;
    @Autowired
    private BookService bookService;
    @Autowired
    private ImgService imgService;

    public void editBrowseHistory(String uCode, String bookCode) {
        BrowseHistoryDto browseHistory = browseHistoryService.getBrowseHistory(uCode, bookCode);
        if (browseHistory == null) {
            BrowseHistory browseHistoryPo = new BrowseHistory();
            BookDto uBookByCode = bookService.getUBookByCode(bookCode);
            browseHistoryPo.setUCode(uCode);
            browseHistoryPo.setBookCode(bookCode);
            browseHistoryPo.setBookPrice(BigDecimal.valueOf(uBookByCode.getBookPrice()));
            browseHistoryPo.setBookTitle(uBookByCode.getBookTitle());
            browseHistoryPo.setFlag((int) uBookByCode.getBookFlag());
            browseHistoryPo.setCtime(new Date());
            List<String> imgByBookCode = imgService.getImgByBookCode(bookCode);
            if (CollectionUtils.isNotEmpty(imgByBookCode)) {
                browseHistoryPo.setImgurl(imgByBookCode.get(0));
            }
            List<BrowseHistoryDto> list = browseHistoryService.getList(uCode);
            if (list.size() == 50){
                Integer id = list.get(list.size() - 1).getId();
                browseHistoryService.deleteByPrimaryKey(id);
            }
            browseHistoryService.insertSelective(browseHistoryPo);
            return;
        }
        browseHistory.setCtime(new Date());
        browseHistoryService.updateByPrimaryKeySelective(BrowseHistoryHelper.covertDtoToPo(browseHistory));
    }

    public List<BrowseHistoryVo> getList(String uCode) {
        List<BrowseHistoryDto> list = browseHistoryService.getList(uCode);
        if (CollectionUtils.isEmpty(list)){
            return Lists.newArrayList();
        }
        return list.stream().map(this::covertDtoToVo).collect(Collectors.toList());
    }

    public void deleteBrowseHistory(Integer id) {
        browseHistoryService.deleteByPrimaryKey(id);
    }

    public void delete(String uCode) {
        browseHistoryService.delete(uCode);
    }

    private BrowseHistoryVo covertDtoToVo(BrowseHistoryDto dto) {
        if (dto == null) {
            return null;
        }
        BrowseHistoryVo vo = new BrowseHistoryVo();
        vo.setId(dto.getId());
        vo.setUCode(dto.getUCode());
        vo.setBookCode(dto.getBookCode());
        vo.setBookTitle(dto.getBookTitle());
        vo.setBookPrice(dto.getBookPrice());
        vo.setImgurl(dto.getImgurl());
        vo.setFlagValue(dto.getFlag() == 1 ? "有效" : "失效");
        vo.setCreateTime(sdf(dto.getCtime()));
        BookDto uBookByCode = bookService.getUBookByCode(vo.getBookCode());
        vo.setBookId(uBookByCode.getId());
        return vo;
    }

    private String sdf(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return date != null ? sdf.format(date) : null;
    }
}
